package com.dwjm.shop.merchant;

import com.dwjm.shop.goods.Goods;
import com.dwjm.shop.utils.ConnectionDatabaseForMySQLUseJDBC;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class MerchantDaoImpl implements MerchantDao{

    Connection connection = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    public MerchantDaoImpl() {
        connection = ConnectionDatabaseForMySQLUseJDBC.getConnection();
    }

    @Override
    public boolean login(String account, String password) {
        try {
            pstmt = connection.prepareStatement("select * from t_merchant where account = ? and password = ?");
            pstmt.setString(1,account);
            pstmt.setString(2,password);
            rs = pstmt.executeQuery();
            if (rs.next()){
                return true;
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public int addGoods(Goods goods) {
        try {
            pstmt = connection.prepareStatement("insert into t_goods(title,price,description,img,state,mid) values(?,?,?,?,?,?)");
            pstmt.setString(1,goods.getTitle());
            pstmt.setBigDecimal(2,goods.getPrice());
            pstmt.setString(3,goods.getDescription());
            pstmt.setString(4,goods.getImg());
            pstmt.setInt(6,0);
            pstmt.setInt(5,goods.getState());
            int i = pstmt.executeUpdate();
            return i == 1 ? 1 : 0;
        }catch (Exception exception){
            exception.printStackTrace();
        }
        return 0;
    }

}
